let selectedFilePath = '';

document.getElementById('selectExe').addEventListener('click', async () => {
  selectedFilePath = await window.electronAPI.selectExe();
  document.getElementById('selectedFile').textContent = selectedFilePath || '未选择文件';
});

document.getElementById('packageForm').addEventListener('submit', async (e) => {
  e.preventDefault();
  const authCode = document.getElementById('authCode').value;
  if (!selectedFilePath || !authCode) {
    showStatus('请选择EXE文件并输入授权码', 'error');
    return;
  }
  
  try {
    const progress = document.getElementById('progress');
    const statusMessage = document.getElementById('statusMessage');
    progress.style.display = 'block';
    statusMessage.style.display = 'none';
    
    const result = await window.electronAPI.encryptAndPackage(selectedFilePath, authCode);
    showStatus(result, 'success');
  } catch (error) {
    showStatus(`打包失败: ${error.message}`, 'error');
  }
});

window.electronAPI.onPackagingProgress((progress) => {
  document.getElementById('progressBar').style.width = `${progress}%`;
  document.getElementById('progressText').textContent = `处理中... ${progress}%`;
});

function showStatus(message, type) {
  const statusMessage = document.getElementById('statusMessage');
  statusMessage.textContent = message;
  statusMessage.className = `status-message ${type}`;
  statusMessage.style.display = 'block';
}
